<template>
  <div>
    <Edialog
      :title="addAeditTitle"
      :outershow="addAeditShow"
      @close="addAeditClose"
      saveClick
      @saveFn="addAeditSaveFn"
    >
      <div style="padding: 0 20px">
        <el-form
          v-if="formShow"
          ref="addAeditRef"
          label-width="170px"
          :model="addAeditFrom"
          :rules="addAeditRules"
        >
          <el-form-item label="对接系统名称" prop="systemName">
            <el-input
              v-model="addAeditFrom.systemName"
              placeholder="请输入"
              maxlength="200"
            ></el-input>
          </el-form-item>
          <el-form-item label="客户端">
            <el-switch
              v-model="addAeditFrom.isClient"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="启用"
              inactive-text="停用"
            >
            </el-switch>
          </el-form-item>
          <el-form-item
            v-if="addAeditFrom.isClient"
            label="客户端应用ID(我方)"
            prop="clientAppId"
          >
            <el-input
              v-model="addAeditFrom.clientAppId"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item
            v-if="addAeditFrom.isClient"
            label="客户端接口地址"
            prop="clientInterfaceUrl"
          >
            <el-input
              v-model="addAeditFrom.clientInterfaceUrl"
              placeholder="请输入"
              maxlength="400"
            ></el-input>
          </el-form-item>
          <el-form-item
            v-if="addAeditFrom.isClient"
            label="客户端加密密钥"
            prop="clientSecretKey"
          >
            <el-input
              v-model="addAeditFrom.clientSecretKey"
              placeholder="请输入"
              maxlength="16"
            ></el-input>
          </el-form-item>
          <el-form-item
            v-if="addAeditFrom.isClient"
            label="客户端加密初始化向量"
            prop="clientSecretLv"
          >
            <el-input
              v-model="addAeditFrom.clientSecretLv"
              placeholder="请输入"
              maxlength="16"
            ></el-input>
          </el-form-item>
          <el-form-item
            v-if="addAeditFrom.isClient"
            label="客户端签名密钥"
            prop="clientSignSecret"
          >
            <el-input
              v-model="addAeditFrom.clientSignSecret"
              placeholder="请输入"
              maxlength="32"
            ></el-input>
          </el-form-item>
          <el-form-item label="服务端">
            <el-switch
              v-model="addAeditFrom.isServer"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="启用"
              inactive-text="停用"
              @change="changeFn"
            >
            </el-switch>
          </el-form-item>
        </el-form>
      </div>
    </Edialog>
  </div>
</template>

<script>
import Edialog from "@/components/Edialog";
import {
  getIfSystemById,
  putIfSystem,
  postIfSystem,
} from "@/api/management/dockingSys";
import { fd } from "@/utils";
export default {
  name: "edit",
  components: {
    Edialog,
  },
  data() {
    return {
      // dialog的标题
      addAeditTitle: "",
      // 是否显示弹框
      addAeditShow: false,
      formShow: true,
      // form表单数据
      addAeditFrom: {
        systemName: "",
        isClient: false,
        clientAppId: "",
        clientInterfaceUrl: "",
        clientSecretKey: "",
        clientSecretLv: "",
        clientSignSecret: "",
        isServer: false,
      },
      // 修改的时候的id
      addAeditId: "",
      // from表单校验规则
      addAeditRules: {
        systemName: [
          { required: true, message: "请输入0-200位", trigger: "change" },
        ],
        clientAppId: [{ required: true, message: "请输入", trigger: "change" }],
        clientInterfaceUrl: [
          { required: true, message: "请输入0-400位", trigger: "change" },
        ],
        clientSecretKey: [
          { required: true, message: "请输入0-16位", trigger: "change" },
        ],
        clientSecretLv: [
          { required: true, message: "请输入0-16位", trigger: "change" },
        ],
        clientSignSecret: [
          { required: true, message: "请输入0-32位", trigger: "change" },
        ],
      },
    };
  },
  created() {},
  methods: {
    // 打开弹框方法
    async open(scope) {
      // 新增
      if (!scope) {
        this.addAeditTitle = "新增";
        this.addAeditShow = true;
        return;
      }
      // 修改
      this.addAeditId = scope.id;
      this.addAeditTitle = "修改";
      let res = await getIfSystemById(scope.id);
      if (res.data && res.data.bdata) {
        let resObj = res.data.bdata;
        this.addAeditFrom.systemName = resObj.systemName;
        this.addAeditFrom.isClient = resObj.isClient == 1 ? true : false;
        this.addAeditFrom.clientAppId = resObj.clientAppId;
        this.addAeditFrom.clientInterfaceUrl = resObj.clientInterfaceUrl;
        this.addAeditFrom.clientSecretKey = resObj.clientSecretKey;
        this.addAeditFrom.clientSecretLv = resObj.clientSecretLv;
        this.addAeditFrom.clientSignSecret = resObj.clientSignSecret;
        this.addAeditFrom.isServer = resObj.isServer == 1 ? true : false;
        this.addAeditShow = true;
      }
    },
    // 关闭弹框方法
    addAeditClose() {
      this.addAeditFrom.systemName = "";
      this.addAeditFrom.isClient = false;
      this.addAeditFrom.clientAppId = "";
      this.addAeditFrom.clientInterfaceUrl = "";
      this.addAeditFrom.clientSecretKey = "";
      this.addAeditFrom.clientSecretLv = "";
      this.addAeditFrom.clientSignSecret = "";
      this.addAeditFrom.isServer = false;
      this.addAeditId = "";
      this.$nextTick(() => this.$refs.addAeditRef.clearValidate());
      this.addAeditShow = false;
    },
    // 保存事件
    addAeditSaveFn() {
      this.$refs.addAeditRef.validate(async (valid) => {
        if (valid) {
          let res;
          let par = JSON.parse(JSON.stringify(this.addAeditFrom));
          par.isClient = par.isClient ? 1 : 0;
          par.isServer = par.isServer ? 1 : 0;
          // 修改
          if (this.addAeditId) {
            par.id = this.addAeditId;
            res = await postIfSystem(fd(par));
          } else {
            // 新增
            res = await putIfSystem(fd(par));
          }
          if (res.data && res.data.bcode == 0) {
            this.$parent.getData();
            this.addAeditClose();
            this.$message.success(res.data.bmsg);
          }
        }
      });
    },
    changeFn(val) {
      if (val) {
        // console.log(123)
        // this.$refs.addAeditRef.clearValidate()
        this.formShow = false;
        this.$nextTick(() => (this.formShow = true));
      }
    },
  },
};
</script>

<style scoped lang="scss"></style>
